package com.xiaotao.learn.algorithm.sort;

import java.util.*;

public class MinK {
    public static void main(String[] args) {
        PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> b - a);
        int[] arr = new int[]{93, 76, 5, 82, 34, 50, 69, 69, 90, 16, 78, 89, 83, 40, 81, 48, 86, 47, 85, 43};
        for (int i = 0; i < 20; i++) {
//            arr[i] = r.nextInt(100);
            if (q.size() < 5) {
                q.offer(arr[i]);
            } else if(arr[i] < q.peek()) {
                q.poll();
                q.offer(arr[i]);
            }
        }
        System.out.print("[");
        for(Integer n: q) {
            System.out.print(n + ", ");
        }
        System.out.println("]");
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        System.out.println();
    }
}
